package com.hmall.item.listener;

import com.hmall.item.domain.po.Item;
import com.hmall.item.service.IItemService;
import com.hmall.item.util.ItemSyncEsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.hmall.common.Constants.MqConstants.*;

@Component
@Slf4j
public class ItemStatusListener {
    @Autowired
    private IItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ItemSyncEsUtil itemSyncEsUtil;



    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = ITEM_SAVE_QUEUE, durable = "true"),
            exchange = @Exchange(name = ITEM_EXCHANGE),
            key = ITEM_SAVE_KEY
    ))
    public void itemSave(Long id) {
        Item item = itemService.getById(id);
        itemSyncEsUtil.saveItemToEs(item);

    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = ITEM_DELETE_QUEUE, durable = "true"),
            exchange = @Exchange(name = ITEM_EXCHANGE),
            key = ITEM_DELETE_KEY
    ))
    public void itemDelete(Long id) {
        itemSyncEsUtil.deleteItemFromEs(id);
    }
}
